//
//  HomeCollectionViewCellTwo.m
//  GiftSay
//
//  Created by apple on 16/1/10.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HomeCollectionViewCellTwo.h"
#import "HomeNewModel.h"
@interface HomeCollectionViewCellTwo ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;
//-------刷新
//tableview方向判断
@property (nonatomic, assign) CGFloat contenty;
@property (nonatomic, strong) UIView *downLoad;
//box
@property (nonatomic, strong) UIImageView *box;
//heart
@property (nonatomic, strong) UIImageView *heart;
//boxarray
@property (nonatomic, strong) NSMutableArray *boxArray;
@end

//-------刷新
//限制一次刷新
static int scrolltimes = 0;
//现在数据个数
static int numOfAll = 0;
//-------

@implementation HomeCollectionViewCellTwo


-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [[NSMutableArray alloc] initWithCapacity:0];
        self.tableview = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableview registerClass:[LongTableViewCell class] forCellReuseIdentifier:@"LongTableViewCell"];
        [self addSubview:self.tableview];
        
        //    下拉刷新
        self.downLoad = [[UIView alloc] initWithFrame:CGRectZero];
        self.downLoad.backgroundColor = [UIColor clearColor];
        [self addSubview:self.downLoad];
        [self bringSubviewToFront:self.downLoad];
        
        self.box = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.box.image = [UIImage imageNamed:@"box_01.png"];
        [self.downLoad addSubview:self.box];
        
        self.heart = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.heart.image = [UIImage imageNamed:@"heart.png"];
        self.heart.alpha = 0;
        [self.downLoad addSubview:self.heart];
        
        self.boxArray = [[NSMutableArray alloc] initWithCapacity:0];
        for (int i = 1; i < 6; i++) {
            NSString *str = [[NSString alloc] initWithFormat:@"box_%02d.png", i];
            UIImage *image = [UIImage imageNamed:str];
            [self.boxArray addObject:image];
        }
        for (int i = 0; i < 30; i++) {
            UIImage *image = [UIImage imageNamed:@"box_05.png"];
            [self.boxArray addObject:image];
        }
        for (int i = 4; i > 0; i--) {
            NSString *str = [[NSString alloc] initWithFormat:@"box_%02d.png", i];
            UIImage *image = [UIImage imageNamed:str];
            [self.boxArray addObject:image];
        }

    }
    return self;
}

-(void)setIdUrl:(NSNumber *)idUrl {
    _idUrl = idUrl;
    [self neWorking:self.idUrl withOffSet:[NSString stringWithFormat:@"%d", numOfAll] withTimes:0];
}

#pragma mark -- tableview协议
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LongTableViewCell"];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.width / 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSLog(@"%@", [self.dataArray[indexPath.row] url]);
    NSString *url = (NSString *)[self.dataArray[indexPath.row] ids];
    [self.delegate jump:url];
}


#pragma mark -- 上拉加载
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.contenty = scrollView.contentOffset.y;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > self.contenty) {
        if (scrollView.contentOffset.y > scrollView.contentSize.height - 700) {
            //加载更多数据
            if (scrolltimes == 0) {
                NSLog(@"加载数据");
                numOfAll = numOfAll + 20;
                [self neWorking:self.idUrl withOffSet:[NSString stringWithFormat:@"%d", numOfAll] withTimes:1];
                scrolltimes = 1;
            }
        }
    }
    if (scrollView.contentOffset.y < 0) {
        self.downLoad.frame = CGRectMake(0, 0, self.width, 0 - scrollView.contentOffset.y);
        self.box.frame = CGRectMake((self.width - self.width / 10) / 2, 0 - scrollView.contentOffset.y - self.width / 10 / 1.23, self.width / 10, self.width / 10 / 1.23);
        self.heart.frame = CGRectMake((self.width - self.box.width / 4 * 2) / 2, 0 - scrollView.contentOffset.y - self.width / 10 / 1.23, self.box.width / 4 * 2, self.box.height / 4 * 3);
    }
}

#pragma mark -- 下拉刷新
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < -60) {
        [scrollView setContentOffset:CGPointMake(0, -60) animated:YES];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < 0) {
        NSLog(@"下拉刷新");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.3 animations:^{
                [UIView setAnimationDelay:0];
                self.heart.frame = CGRectMake((self.width - self.box.width / 4 * 2) / 2, 0 - scrollView.contentOffset.y - self.width / 10 / 1.23 - self.box.height / 4 * 3, self.box.width / 4 * 2, self.box.height / 4 * 3);
                self.heart.alpha = 1;
            } completion:^(BOOL finished) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.heart.alpha = 0;
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.tableview setContentOffset:CGPointMake(0, 0) animated:YES];
                    });
                });
            }];
        });
        
        [self.box setAnimationImages:self.boxArray];
        [self.box setAnimationDuration:1];
        [self.box setAnimationRepeatCount:1];
        [self.box startAnimating];
        
        numOfAll = 0;
        [self.dataArray removeAllObjects];
        [self neWorking:self.idUrl withOffSet:[NSString stringWithFormat:@"%d", numOfAll] withTimes:1];
    }
    
}

-(void)neWorking:(NSNumber *)ids withOffSet:(NSString *)offset withTimes:(NSInteger)a {
    NSString *url = [NSString stringWithFormat:KHomeOtherAll, ids, offset];
    [LYKNetWorkTool getWithUrl:url parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        if (a == 0) {
            [self.dataArray removeAllObjects];
            [self.tableview setContentOffset:CGPointMake(0, 0) animated:NO];
        }
        NSDictionary *allDic = [[NSDictionary alloc] initWithDictionary:result];
        NSDictionary *bannerDic = allDic[@"data"];
        NSArray *AllArray = bannerDic[@"items"];
        for (NSDictionary *dic in AllArray) {
            HomeNewModel *model = [[HomeNewModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        scrolltimes = 0;
        [self.tableview reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.tableview.frame = self.bounds;
}

@end
